package com.bosssoft.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.po.FibExpenseBudgetPO;
import com.bosssoft.entity.vo.FibIncomeBudgetVO;
import com.bosssoft.entity.vo.FibTrainBudgetVO;
import com.bosssoft.feign.DepartmentFeignService;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibBudgetService;
import com.bosssoft.service.FibExpenseBudgetService;
import com.bosssoft.service.FibIncomeBudgetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author Li kexi
 * Created on 2021/8/17.16:37
 */
@RestController
@Api(tags = "预算处（科、股）用户-预算管理控制器")
public class BureauFibBudgetController {

    /**
     * 定额管理 （指标管理）
     * 单位属性维护 （在单位管理器中实现）
     * 基础数据导入 （部门经济分类、政府经济分类、政府经济功能科目）
     * 预算数据审阅 （不理解）
     * 预算审核（改状态、发通知）
     * 预算提交（最终提交给人大）
     * 预算数据的备份和恢复（可选）
     * 报表查询（不理解）
     */

    @Autowired
    private FibExpenseBudgetService fibExpenseBudgetService;

    @Autowired
    private FibIncomeBudgetService fibIncomeBudgetService;

    @Autowired
    private FibBudgetService fibBudgetService;


    // 查看收支总表
    // 采用两个接口，将结果拼接展示即可
    @PostMapping("/banlanceSheet/expense")
    @ApiOperation("查看某年支出预算总表")
    public Result expenseSheet(Integer year) {
        // 进库把所有的已经最后一级审核通过的（状态为4）的记录查出来 expense_budget
        Integer status = 4;
        Map<Integer, BigDecimal> result = fibExpenseBudgetService.expenseSheet(status, year);
        // Map<经济科目代码，金额>
        return Result.ok().data("expenseSheet", result);

    }

    @PostMapping("/banlanceSheet/income")
    @ApiOperation("查看某年收入预算总表")
    public Result incomeSheet(Integer year) {
        //进库查出所有状态为 4 的记录 income_budget
        Integer status = 4;
        Map<String, BigDecimal> result = fibIncomeBudgetService.incomeSheet(status, year);
        return Result.ok().data("incomeSheet", result);
    }

    @PostMapping("/checkAndApprove")
    @ApiOperation("预算数据审阅")
    public Result checkAndApprove() {
        //todo:
        return null;
    }

    // 1 -> 初始
    // 2 -> 二级部门审核通过
    // 3 -> 不通过，让申报部门修改报表
    // 4 -> 一级部门审核通过
    // 5 -> 申报部门修改报表

    // 这一级只负责审核 状态为 2 的报表
    @PostMapping("/auditExpense")
    @ApiOperation("审核某单位的年度支出报表（一上 II）")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "deptId", name = "部门id"),
            @ApiImplicitParam(value = "year", name = "年度"),
            @ApiImplicitParam(value = "nextStatus", name = "下一状态")
    })
    public Result auditExpense(Long deptId, Integer year, Integer nextStatus) {

        // 2 -> 待预算处审核状态
        Map<Long, List<FibExpenseBudgetPO>> map = fibExpenseBudgetService.getAllStatement(deptId, year, 2);
        List<FibExpenseBudgetPO> expenseBudgetPOS = map.get(deptId);
        for (FibExpenseBudgetPO expenseBudgetPO : expenseBudgetPOS) {
            fibExpenseBudgetService.changeStatus(expenseBudgetPO, nextStatus);
        }
        // 审核不通过，要发操作意见
        // 审核通过，需要给项昊天fib_budget新增数据
        Map<Long, BigDecimal> mapForDeptId = fibExpenseBudgetService.expenseSheet1(4, year);
        boolean save = fibBudgetService.createFromLastCheck(mapForDeptId);
        return Result.ok();
    }

    @PostMapping("/queryAllIncome")
    @ApiOperation("查询所有单位的收入预算")
    public Result queryAllIncome(@RequestParam(defaultValue = "1") Integer pageNo,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<FibIncomeBudgetVO> page = fibIncomeBudgetService.queryAllIncome(pageNo, pageSize);
        PageResult<FibIncomeBudgetVO> pageResult = new PageResult<>(page, page.getRecords());
        return Result.ok().data("page", pageResult);
    }
}
